﻿namespace Codeblack.TfsScrumboard.Runtime
{
    using System;
    using System.Linq;
    using System.Web;

    /// <summary>
    /// Extends the tab-header of TFS Web Access with the tabs needed for this application.
    /// </summary>
    public class WebAccessTabExtenderModule : IHttpModule
    {
        #region Constants

        private const string HttpGetMethod = "GET";
        private const string ASPNETPageExtension = ".aspx";
        private const string HtmlContentType = "text/html";
        private readonly string[] ExcludedPages = new string[]
        {
        };

        #endregion

        #region IHttpModule implementation

        /// <summary>
        /// Initializes this module and prepares it to handle requests.
        /// </summary>
        /// <param name="context">An System.Web.HttpApplication that provides access to the methods, properties,
        /// and events common to all application objects within an ASP.NET application</param>
        public void Init(HttpApplication context)
        {
            context.BeginRequest += new EventHandler(OnBeginRequest);
        }

        /// <summary>
        /// Disposes of the resources (other than memory) used by the module that implements System.Web.IHttpModule.
        /// </summary>
        public void Dispose()
        {
        }

        #endregion

        #region Event handlers

        /// <summary>
        /// Occurs as the first event in the HTTP pipeline chain of execution when ASP.NET responds to a request.
        /// Registers a filter on the response which will extend the tab-header.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void OnBeginRequest(object sender, EventArgs e)
        {
            // get a reference to the current request
            HttpRequest request = HttpContext.Current.Request;

            // determine information about the current request
            bool isGet = request.HttpMethod.ToUpperInvariant() == HttpGetMethod;
            bool isAspx = request.CurrentExecutionFilePath.EndsWith(ASPNETPageExtension);
            bool isExcluded = (from excludedPage in ExcludedPages
                               where request.FilePath.ToLowerInvariant().Contains(excludedPage)
                               select excludedPage).Count() > 0;

            // only proceed if the request is a GET for an aspx page and it's not one the excluded-pages
            if (isGet && isAspx && !isExcluded)
            {
                // get a reference to the response to the current request
                HttpResponse response = HttpContext.Current.Response;

                // if the request is for HTML content
                if (response.ContentType == HtmlContentType)
                {
                    // create the filter
                    WebAccessTabExtenderFilter filter = new WebAccessTabExtenderFilter(response.Filter, (HttpApplication)sender);
                    
                    // the filter has no dependencies, no reason for dependecy-injection for now
                    //Container.Instance.BuildUp(typeof(WebAccessTabExtenderFilter), filter, null);

                    // register the filter on the response
                    response.Filter = filter;
                }
            }
        }

        #endregion
    }
}
